package com.dingding.special.order.aboard.biz.impl;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.model.push.PushModel;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.paravalidate.ValiResult;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.facade.queue.dto.push.PushRequestDTO;
import com.dingding.facade.queue.enums.ClientType;
import com.dingding.facade.queue.enums.PushType;
import com.dingding.facade.queue.facade.IQueueFacade;
import com.dingding.order.dto.publish.PushData;
import com.dingding.special.order.aboard.biz.IConfirmAboardService;
import com.dingding.special.order.aboard.code.ConfirmAboardCodeMsg;
import com.dingding.special.order.aboard.dao.IConfirmAboardDao;
import com.dingding.special.order.aboard.dto.ConfirmAboardNoticeDTO;
import com.dingding.special.order.aboard.dto.ReqConfirmAboardDTO;
import com.dingding.special.order.aboard.dto.ResConfirmAboardDTO;

/**
 * 
 * @ClassName: ConfirmAboardServiceImpl
 * @Description: 确认上车
 * @author YHQ
 * @date 2016年8月21日 下午2:22:43
 * @since V2.4.0
 * 
 */
@Component("confirmAboardService")
public class ConfirmAboardServiceImpl implements IConfirmAboardService {

	private Logger log = Logger.getLogger(this.getClass());

	@Resource(name = "confirmAboardDao")
	private IConfirmAboardDao confirmAboardDao;

	@Resource(name = "queueService")
	private IQueueFacade queueFacade;

	/**
	 * 
	 * @Title: confirmAboard
	 * @Description: 确认上车
	 * @param reqConfirmAboardDTO
	 *            确认上车请求参数
	 * @author YHQ
	 * @since V2.4.0
	 * @return ResponseEntity<ResConfirmAboardDTO> 返回类型
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseEntity<ResConfirmAboardDTO> confirmAboard(ReqConfirmAboardDTO reqConfirmAboardDTO) {

		// 确认上车响应Bean
		ResponseEntity<ResConfirmAboardDTO> response = new ResponseEntity<ResConfirmAboardDTO>();

		// 是否符合确认上车条件
		boolean flag = this.validateParamAndCondition(response, reqConfirmAboardDTO);

		if (!flag) {
			return response;
		}

		// 修改订单状态
		boolean operationResult = this.confirmAboardDao.confirmAboardOperation(response, reqConfirmAboardDTO);

		if (operationResult) {
			// 通知乘客
			this.confirmAboardNotice(reqConfirmAboardDTO);
		}

		return response;
	}

	/**
	 * 
	 * @Title: validateParamAndCondition
	 * @Description: 验证参数
	 * @param response
	 *            确认上车响应Bean
	 * @param reqConfirmAboardDTO
	 *            确认上车请求参数
	 * @author YHQ
	 * @return boolean 返回类型
	 * 
	 */
	public boolean validateParamAndCondition(ResponseEntity<ResConfirmAboardDTO> response,
			ReqConfirmAboardDTO reqConfirmAboardDTO) {

		// 判断客户端请求数据是否为空
		ValiResult valiResult = ValidateUtil.validateBeanIsAvaliable(reqConfirmAboardDTO, ReqConfirmAboardDTO.class);

		if (!valiResult.isAvailable()) {
			response.setCode(HttpStatus.SC_FORBIDDEN);
			response.setErrorMessage(ConfirmAboardCodeMsg.DATA_NULL_PARAM_MESSAGE);// 客户端请求数据为空
			response.setPromptMessage(ConfirmAboardCodeMsg.APP_ERROR);// 客户端请求数据为空
			response.setNullProperties(valiResult.getNullFieldName());
			log.error("为空字段为：" + valiResult.getNullFieldName());
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: confirmAboardNotice
	 * @Description: 确认上车通知操作
	 * @param reqConfirmAboardDTO
	 *            确认上车客户端请求参数
	 * @author YHQ
	 * @since V2.4.0
	 * @return void 返回类型
	 * 
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	public void confirmAboardNotice(ReqConfirmAboardDTO reqConfirmAboardDTO) {

		// 确认上车通知DTO
		ConfirmAboardNoticeDTO confirmAboardNoticeDTO = confirmAboardDao.getNoticeInfo(reqConfirmAboardDTO.getOid());

		if (confirmAboardNoticeDTO != null) {

			// 推送消息
			this.confirmAboardPushMessage(confirmAboardNoticeDTO);

			// 司机确认上车-通知推送
			this.confirmAboardPushNotice(confirmAboardNoticeDTO);
		}

	}

	/**
	 * 
	 * @Title: confirmAboardPushMessage
	 * @Description: 确认上车后推送消息
	 * @param confirmAboardNoticeDTO
	 *            确认上车，客户端请求参数
	 * @author YHQ
	 * @since V2.4.0
	 * @return void 返回类型
	 * 
	 */
	public void confirmAboardPushMessage(ConfirmAboardNoticeDTO confirmAboardNoticeDTO) {

		// 推送Bean
		PushData pushData = new PushData();

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("specialBboardPush");
		pushData.setResponseBody(confirmAboardNoticeDTO);

		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(confirmAboardNoticeDTO.getAliasId());
		pushRequestDTO.setAlert("确认上车");

		// 设备类型
		if (confirmAboardNoticeDTO.getClientType().equals("Android")) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (confirmAboardNoticeDTO.getClientType().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.alert_msgcontent);

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData));

		System.out.println("确认上车推送内容：" + JSONUtil.toJSONString(pushRequestDTO));

		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));
	}

	/**
	 * 
	 * @Title: confirmAboardPushNotice
	 * @Description: 确认上车后，通知推送消息
	 * @param confirmAboardNoticeDTO
	 *            确认上车，客户端请求参数
	 * @author YHQ
	 * @since V2.4.0
	 * @return void 返回类型
	 * 
	 */
	public void confirmAboardPushNotice(ConfirmAboardNoticeDTO confirmAboardNoticeDTO) {

		// 推送Bean
		PushData pushData = new PushData();

		String content = "车主已确认上车";

		// 推送序号(时间戳)
		pushData.setRequestNumber(DateFormatUtil.getTimeInMillis().toString());
		// 推送时间
		pushData.setRequestTime(DateFormatUtil.yyyy_MM_dd_HHmmss());
		// 模块
		pushData.setModular("newsNoticePush");
		// 确认到达推送内容
		pushData.setResponseBody(PushModel.noticeModel(1, content, null));

		PushRequestDTO pushRequestDTO = new PushRequestDTO();

		// 推送别名
		pushRequestDTO.setAliasId(confirmAboardNoticeDTO.getAliasId());

		// 设备类型
		if (confirmAboardNoticeDTO.getClientType().equals("Android")) {
			pushRequestDTO.setClientType(ClientType.Android);
		} else if (confirmAboardNoticeDTO.getClientType().equals("iOS")) {
			pushRequestDTO.setClientType(ClientType.iOS);
		}

		// 消息推送类型
		pushRequestDTO.setPushType(PushType.msgcontent);

		// 推送类型
		pushRequestDTO.setPushContent(JSONUtil.toJSONString(pushData));

		System.out.println("确认上车通知内容：" + JSONUtil.toJSONString(pushRequestDTO));
		queueFacade.addPushToQueue(JSONUtil.toJSONString(pushRequestDTO));
	}

}
